package com.ev.hr.service.impl;

import com.ev.hr.dao.SalaryArchivesDao;
import com.ev.hr.domain.SalaryArchivesDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.ev.hr.dao.SalaryChangeDao;
import com.ev.hr.domain.SalaryChangeDO;
import com.ev.hr.service.SalaryChangeService;
import com.ev.framework.utils.R;
import com.google.common.collect.Maps;


@Service
public class SalaryChangeServiceImpl implements SalaryChangeService {
	@Autowired
	private SalaryChangeDao salaryChangeDao;
	@Autowired
	private SalaryArchivesDao salaryArchivesDao;
	
	@Override
	public SalaryChangeDO get(Long id){
		return salaryChangeDao.get(id);
	}
	
	@Override
	public List<SalaryChangeDO> list(Map<String, Object> map){
		return salaryChangeDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return salaryChangeDao.count(map);
	}
	
	@Override
	public int save(SalaryChangeDO salaryChange){
		return salaryChangeDao.save(salaryChange);
	}
	
	@Override
	public int update(SalaryChangeDO salaryChange){
		return salaryChangeDao.update(salaryChange);
	}

	@Override
	public int updateAll(SalaryChangeDO salaryChange){
		return salaryChangeDao.updateAll(salaryChange);
	}
	
	@Override
	public int remove(Long id){
		return salaryChangeDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return salaryChangeDao.batchRemove(ids);
	}

	@Override
	public R saveAndVerify(SalaryChangeDO saveParam){
		Long employeeId = saveParam.getEmployeeId();
		Map<String,Object> param = Maps.newHashMap();
		param.put("employeeId",employeeId);
		List<SalaryArchivesDO> salaryArchives = salaryArchivesDao.list(param);
		if (salaryArchives.size() > 0) {
			SalaryArchivesDO salaryArchivesDO = salaryArchives.get(0);
			salaryArchivesDO.setPostFixedLevel(saveParam.getPostFixedLevel());
			salaryArchivesDao.update(salaryArchivesDO);
			salaryChangeDao.save(saveParam);
			return R.ok();
		}
		return R.error();
	}

	@Override
	public Map<String, Object> getById(Long id) {
		SalaryChangeDO salaryChange = this.salaryChangeDao.get(id);
		if (salaryChange != null) {
			Map<String,Object> result = Maps.newHashMap();
			result.put("data",salaryChange);
			return R.ok(result);
		}
		return R.error();
	}

}
